/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminator.levels.effects;

import com.jme3.app.state.AbstractAppState;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import emitter.Emitter;
import emitter.EmitterMesh;
import emitter.influencers.AlphaInfluencer;
import emitter.influencers.ColorInfluencer;
import emitter.influencers.GravityInfluencer;
import emitter.influencers.SizeInfluencer;
import emitter.particle.ParticleDataImpostorMesh;
import exterminator.Main;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author t0neg0d
 */
public class Effects extends AbstractAppState {
	Main main;
	List<Text> texts = new ArrayList();
	List<Text> remove = new ArrayList();
	Quaternion q = new Quaternion();
	Emitter splashEmitter;
	
	public Effects(Main main) {
		this.main = main;
		
		initSplashEmitter();
	}
	
	private void initSplashEmitter() {
		splashEmitter = new Emitter();
		splashEmitter.setName("eGravity1");
		splashEmitter.setMaxParticles(240);
		splashEmitter.addInfluencers(
			new GravityInfluencer(),
			new ColorInfluencer(),
			new AlphaInfluencer(),
			new SizeInfluencer()
		);
		splashEmitter.setShapeSimpleEmitter();
		splashEmitter.setParticleType(ParticleDataImpostorMesh.class);
		splashEmitter.setSprite("Textures/glow.png");
		splashEmitter.setEmissionsPerSecond(0);
		splashEmitter.setParticlesPerEmission(2);
		splashEmitter.setForce(5.2f);
		splashEmitter.setLife(.5f);
		splashEmitter.setBillboardMode(Emitter.BillboardMode.Velocity_Z_Up);
		splashEmitter.setEmitterTestMode(false, false);
		splashEmitter.setUseVelocityStretching(true);
		splashEmitter.setVelocityStretchFactor(1.45f);
		splashEmitter.setUseRandomEmissionPoint(true);
		
		splashEmitter.getInfluencer(GravityInfluencer.class).setGravity(new Vector3f(0,0,0));
		splashEmitter.getInfluencer(GravityInfluencer.class).setAlignment(GravityInfluencer.GravityAlignment.Reverse_Velocity);
		splashEmitter.getInfluencer(GravityInfluencer.class).setMagnitude(6.5f);
		
		splashEmitter.getInfluencer(ColorInfluencer.class).addColor(new ColorRGBA(1f,0.75f,0f,0.45f));
		splashEmitter.getInfluencer(ColorInfluencer.class).addColor(new ColorRGBA(1f,1f,9f,0.45f));
		
		splashEmitter.getInfluencer(SizeInfluencer.class).addSize(.05f);
		
		splashEmitter.setLocalTranslation(0,0,0);
		
		splashEmitter.initialize(main.getAssetManager());
		
		setConfig1();
		
		splashEmitter.reset();
		main.getRootNode().addControl(splashEmitter);
		splashEmitter.setEnabled(false);
		
	//	splashEmitter.getMaterial().getAdditionalRenderState().setBlendMode(RenderState.BlendMode.AlphaAdditive);
	//	splashEmitter.getParticleNode().setQueueBucket(RenderQueue.Bucket.Translucent);
	}
	
	public void setConfig1() {
		splashEmitter.setEnabled(false);
		splashEmitter.killAllParticles();
		
		splashEmitter.setShapeSimpleEmitter();
		splashEmitter.setDirectionType(EmitterMesh.DirectionType.RandomNormalAligned);
		splashEmitter.setBillboardMode(Emitter.BillboardMode.Velocity_Z_Up_Y_Left);
		splashEmitter.setForce(5.2f);
		splashEmitter.setEmissionsPerSecond(0);
		splashEmitter.setParticlesPerEmission(2);
		splashEmitter.setUseRandomEmissionPoint(false);
		
		splashEmitter.getInfluencer(GravityInfluencer.class).setGravity(new Vector3f(0,12,0));
		splashEmitter.getInfluencer(GravityInfluencer.class).setAlignment(GravityInfluencer.GravityAlignment.World);
		
		splashEmitter.setEnabled(true);
	}
	
	public void addTextEffect(String text, Vector3f pos, ColorRGBA color, boolean rotate) {
		BitmapText bt = new BitmapText(main.getDefaultFont());
		bt.setText(text);
		bt.setSize(1.25f);
		bt.setLocalTranslation(pos);
		bt.setColor(color);
		bt.getChild(0).setLocalTranslation(-(bt.getLineWidth()/2), bt.getHeight(), 0);
		((Geometry)bt.getChild(0)).getMaterial().getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
		bt.setQueueBucket(RenderQueue.Bucket.Transparent);
		main.getRootNode().attachChild(bt);
		
		Text t = new Text(bt, rotate);
		texts.add(t);
	}
	
	public void addSplashEffect(Vector3f pos, ColorRGBA color) {
		splashEmitter.getInfluencer(ColorInfluencer.class).addColor(new ColorRGBA(color.r,color.g,color.b,0.45f));
		splashEmitter.getInfluencer(ColorInfluencer.class).addColor(new ColorRGBA(1f,1f,1f,0.45f));
		
		
		splashEmitter.setLocalTranslation(pos);
		splashEmitter.emitNumParticles(40);
		
		splashEmitter.setEnabled(true);
	}
	
	@Override
	public void update(float tpf) {
		if (splashEmitter.isEnabled())
			if (splashEmitter.getActiveParticleCount() <= 0)
				splashEmitter.setEnabled(false);
		
		for (Text text : texts) {
			text.time += tpf;
			text.rot += (tpf*8);
			
			if (text.rotate)
				text.text.setLocalRotation(q.fromAngleAxis(text.rot, Vector3f.UNIT_Y));
			
			text.text.move(0,(tpf*5),0);
			
			text.alpha -= tpf;
			text.text.setAlpha(text.alpha);
			
			if (text.time > 1) {
				text.text.removeFromParent();
				remove.add(text);
			}
		}
		
		if (!remove.isEmpty()) {
			texts.removeAll(remove);
			remove.clear();
		}
	}
	
	public class Text {
		public BitmapText text;
		public float rot = 0;
		public float time = 0;
		public float alpha = 1;
		public boolean rotate;
		public Text(BitmapText text, boolean rotate) {
			this.text = text;
			this.rotate = rotate;
		}
	}
}
